"use strict"
var db = require('../db-connection');
const Review = require('../Models/review');
function updateOneStar(id,twoStar) {
    console.log("2222")
    var sql = "select avg(review_star) as star from review"
   
    db.query(sql,function(err,result){
        if(!err){
            console.log(result[0].star)
            console.log(findfirstStar(id)+"----")
            console.log(twoStar+"----00")
            // console.log(((findfirstStar(id)+twoStar)/2))
            updateStar(result[0].star==null?((findfirstStar(id)+twoStar)/2).toFixed(1):result[0].star.toFixed(1),id)
        }
    })
}
function findfirstStar(id){
    var sql = `select * from restaurant where Restaurant_id = ${id}`
    db.query(sql,function(err,result){
        if(!err){
            // console.log(result[0])
            return result[0].star
        }
    })
}
function updateStar(count,id){
    console.log(count+"--------最后的计算结果")
    var sql = `update restaurant set star = ${count} where Restaurant_id = ${id}`
    db.query(sql,function(err,result){
        if(!err){
            console.log("重新计算饭店评分")
        }
    })
}
class reviewDB {
    getAllReview(request, respond){
        var sql = "SELECT lastname, login_review, review_star, review_date, review_time FROM login INNER JOIN review ON login.login_id = review.login_id ";
        db.query(sql, function(error, result){
            if(error){
                throw error;
            }
            else{
                respond.json(result);
            }
        });
    }
    findOneReview(req,res){
        // var sql = `SELECT * FROM  areyouhungry.review  where Restaurant_id =  ${req.body.id};`
        var sql = `select * from login join review on login.login_id = review.login_id where Restaurant_id =  ${req.body.id};`
        db.query(sql, function(error, result){
            if(error){
                throw error;
            }
            else{
                // updateOneStar()
                // updateOneStar(req.body.id)
                res.json(result);
            }
        });

    }
    


    addReview(request, respond){
        var reviewObject = new Review(null,request.body.login_review , request.body.review_star , request.body.review_date , request.body.review_time,request.body.Restaurant_id,request.body.login_id);
        // console.log(reviewObject)
        // return
        var sql = "INSERT INTO areyouhungry.review (login_review , review_star , review_date , review_time , Restaurant_id , login_id) VALUES(?,?,?,?,?,?)";        
        var values = [reviewObject.getLogin_review(), reviewObject.getReview_star(), reviewObject.getReview_date(), reviewObject.getReview_time(),reviewObject.getRestaurant_id(),reviewObject.getlogin_id()];
       
        db.query(sql, values, function (error, result) {
                if(error){
                    throw error;
                }
                else{
                    updateOneStar(request.body.Restaurant_id,request.body.review_star)
                    respond.json({status:"1",message:"评论成功"});
                }
              });
        }

    deleteReview(request, respond){
        var reviewID = request.params.id;
        var sql = "DELETE FROM areyouhungry.review WHERE review_id = ?";
        db.query(sql, reviewID, function (error, result) {
            if(error){
                throw error;
            }
            else{
                respond.json(result);
            }
          });
    }
}

module.exports = reviewDB;